
import logging
import inspect
import os
class LogPrint():

    logger=None
    
    def init():
        if LogPrint.logger is None:
            strFormatter='%(asctime)s - %(levelname)s - %(message)s'
            # 配置日志
            logging.basicConfig(level=logging.DEBUG,format=strFormatter)            
            # 创建一个日志器
            LogPrint.logger = logging.getLogger(__name__)
            file_handler = logging.FileHandler('my_log.log')
            formatter = logging.Formatter(strFormatter)
            file_handler.setFormatter(formatter)
            LogPrint.logger.addHandler(file_handler)

    def info(*args):

        LogPrint.init()
        if args is not None:
            msg=''

            # 获取当前帧
            current_frame = inspect.currentframe()
            # 获取调用者的帧
            caller_frame = inspect.getouterframes(current_frame, 2)
            # 获取调用者的信息
            caller_info = caller_frame[1]
            #print(caller_info[1],caller_info[2],caller_info[3])
            

            if caller_info and len(caller_info)>3:
                # 使用os.path.split()分割路径和文件名
                dirname, filename = os.path.split(caller_info[1])
                msg=f"[{filename}-{caller_info[3]}-{caller_info[2]}]--"
            #组合日志
            for arg in args:
                msg+=str(arg)
                msg+=','
            msg=msg[:len(msg)-1]
            LogPrint.logger.info(msg)

    def debug(*args):

        LogPrint.init()
        if args is not None:
            msg=''

            # 获取当前帧
            current_frame = inspect.currentframe()
            # 获取调用者的帧
            caller_frame = inspect.getouterframes(current_frame, 2)
            # 获取调用者的信息
            caller_info = caller_frame[1]
            #print(caller_info[1],caller_info[2],caller_info[3])
            

            if caller_info and len(caller_info)>3:
                # 使用os.path.split()分割路径和文件名
                dirname, filename = os.path.split(caller_info[1])
                msg=f"[{filename}-{caller_info[3]}-{caller_info[2]}]--"
            #组合日志
            for arg in args:
                msg+=str(arg)
                msg+=','
            msg=msg[:len(msg)-1]
            LogPrint.logger.debug(msg)


    def error(*args):

        LogPrint.init()
        if args is not None:
            msg=''

            # 获取当前帧
            current_frame = inspect.currentframe()
            # 获取调用者的帧
            caller_frame = inspect.getouterframes(current_frame, 2)
            # 获取调用者的信息
            caller_info = caller_frame[1]
            #print(caller_info[1],caller_info[2],caller_info[3])
            

            if caller_info and len(caller_info)>3:
                # 使用os.path.split()分割路径和文件名
                dirname, filename = os.path.split(caller_info[1])
                msg=f"[{filename}-{caller_info[3]}-{caller_info[2]}]--"
            #组合日志
            for arg in args:
                msg+=str(arg)
                msg+=','
            msg=msg[:len(msg)-1]
            LogPrint.logger.error(msg)
